package com.shouxin.attendance.database.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.shouxin.attendance.database.dao.BabyDao;
import com.shouxin.attendance.database.dao.ClazzDao;
import com.shouxin.attendance.database.dao.CustodyDao;
import com.shouxin.attendance.database.dao.DaoSession;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinEntity;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.ToOne;

import java.util.List;

@Entity(nameInDb = "t_baby")
public class Baby {

	@Id
	@JSONField(name = "baby_id")
	private Long id;

	@NotNull
	private String name;

	private String head;

	@NotNull
	@JSONField(name = "stay_type")
	private Integer type;

	private Integer sex;

	@JSONField(name = "class_id")
	private Long classId;

	@ToOne(joinProperty = "classId")
	private Clazz clazz;

	private String card;

	@ToMany
	@JoinEntity(entity = BabyCustody.class, sourceProperty = "babyId", targetProperty = "custodyId")
	private List<Custody> custodys;

	/** Used to resolve relations */
	@Generated(hash = 2040040024)
	private transient DaoSession daoSession;

	/** Used for active entity operations. */
	@Generated(hash = 343306160)
	private transient BabyDao myDao;

	@Generated(hash = 1210854875)
	private transient Long clazz__resolvedKey;

	@Generated(hash = 1135543135)
	public Baby(Long id, @NotNull String name, String head, @NotNull Integer type,
	            Integer sex, Long classId, String card) {
		this.id = id;
		this.name = name;
		this.head = head;
		this.type = type;
		this.sex = sex;
		this.classId = classId;
		this.card = card;
	}

	@Generated(hash = 1845915441)
	public Baby() {
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHead() {
		return this.head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getSex() {
		return this.sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public Long getClassId() {
		return this.classId;
	}

	public void setClassId(Long classId) {
		this.classId = classId;
	}

	public String getCard() {
		return this.card;
	}

	public void setCard(String card) {
		this.card = card;
	}

	/** To-one relationship, resolved on first access. */
	@Generated(hash = 276623111)
	public Clazz getClazz() {
		Long __key = this.classId;
		if (clazz__resolvedKey == null || !clazz__resolvedKey.equals(__key)) {
			final DaoSession daoSession = this.daoSession;
			if (daoSession == null) {
				throw new DaoException("Entity is detached from DAO context");
			}
			ClazzDao targetDao = daoSession.getClazzDao();
			Clazz clazzNew = targetDao.load(__key);
			synchronized (this) {
				clazz = clazzNew;
				clazz__resolvedKey = __key;
			}
		}
		return clazz;
	}

	/** called by internal mechanisms, do not call yourself. */
	@Generated(hash = 382587439)
	public void setClazz(Clazz clazz) {
		synchronized (this) {
			this.clazz = clazz;
			classId = clazz == null ? null : clazz.getId();
			clazz__resolvedKey = classId;
		}
	}

	/**
	 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
	 * Entity must attached to an entity context.
	 */
	@Generated(hash = 128553479)
	public void delete() {
		if (myDao == null) {
			throw new DaoException("Entity is detached from DAO context");
		}
		myDao.delete(this);
	}

	/**
	 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
	 * Entity must attached to an entity context.
	 */
	@Generated(hash = 1942392019)
	public void refresh() {
		if (myDao == null) {
			throw new DaoException("Entity is detached from DAO context");
		}
		myDao.refresh(this);
	}

	/**
	 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
	 * Entity must attached to an entity context.
	 */
	@Generated(hash = 713229351)
	public void update() {
		if (myDao == null) {
			throw new DaoException("Entity is detached from DAO context");
		}
		myDao.update(this);
	}

	/** called by internal mechanisms, do not call yourself. */
	@Generated(hash = 1918449393)
	public void __setDaoSession(DaoSession daoSession) {
		this.daoSession = daoSession;
		myDao = daoSession != null ? daoSession.getBabyDao() : null;
	}

	/**
	 * To-many relationship, resolved on first access (and after reset).
	 * Changes to to-many relations are not persisted, make changes to the target entity.
	 */
	@Generated(hash = 593255092)
	public List<Custody> getCustodys() {
		if (custodys == null) {
			final DaoSession daoSession = this.daoSession;
			if (daoSession == null) {
				throw new DaoException("Entity is detached from DAO context");
			}
			CustodyDao targetDao = daoSession.getCustodyDao();
			List<Custody> custodysNew = targetDao._queryBaby_Custodys(id);
			synchronized (this) {
				if (custodys == null) {
					custodys = custodysNew;
				}
			}
		}
		return custodys;
	}

	public void setCustodys(List<Custody> custodys) {
		this.custodys = custodys;
	}

	/** Resets a to-many relationship, making the next get call to query for a fresh result. */
	@Generated(hash = 624062862)
	public synchronized void resetCustodys() {
		custodys = null;
	}

}
